﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Text;
using System.Data;

namespace OtechWebServer
{
    public partial class SendNoPsgWarmingSMS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string phoneNumber = "";
            if (Request.QueryString["PhoneNumber"] != null)
            {
                phoneNumber = Request.QueryString["PhoneNumber"];

                int nowdistance = 0;
                DateTime startTime;
                DateTime endTime;
                StringBuilder strSms = new StringBuilder();
               

                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * from ttaxinopsgdistanceconfig ");
                strSql.Append("where PhoneNumber='");
                strSql.Append(phoneNumber);
                strSql.Append("'");

                MySqlConnection connection = new MySqlConnection();
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconn"].ToString();
                using (MySqlCommand cmd = new MySqlCommand(strSql.ToString(), connection))
                {
                    try
                    {
                        DataSet ds = new DataSet();

                        connection.Open();
                        MySql.Data.MySqlClient.MySqlDataAdapter da =
                            new MySql.Data.MySqlClient.MySqlDataAdapter(strSql.ToString(), connection);
                        da.Fill(ds);

                         
                        if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
                        {
                            DateTime stime1 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime1"].ToString());
                            DateTime etime1 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime1"].ToString());
                            DateTime stime2 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime2"].ToString());
                            DateTime etime2 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime2"].ToString());
                            int distance1 = int.Parse(ds.Tables[0].Rows[0]["Distance1"].ToString());
                            int distance2 = int.Parse(ds.Tables[0].Rows[0]["Distance2"].ToString());

                            strSms.Append("请注意，您");
                            if (etime1.TimeOfDay < stime1.TimeOfDay)//结束时间大于开始时间 则第一个时间为前一天的  22:00 - 8:00
                            {
                                DateTime dt1 = DateTime.Parse(DateTime.Now.AddDays(-1).Date.ToString("yyyy-MM-dd") + " " + stime1.TimeOfDay);
                                string strDate1 = DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + etime1.TimeOfDay;
                                DateTime dt2 = DateTime.Parse(strDate1);//今天时间 如 8：00：00
                                
                                if (DateTime.Now > dt1 && DateTime.Now <= dt2) //昨天晚上到今天白天
                                {
                                    startTime = stime1;
                                    endTime = etime1;
                                    nowdistance = distance1;
                                    strSms.Append("昨天");
                                    strSms.Append(stime1.Hour);
                                    strSms.Append("点");
                                    strSms.Append(stime1.Minute);
                                    strSms.Append("分到今天");
                                    strSms.Append(etime1.Hour);
                                    strSms.Append("点");
                                    strSms.Append(etime1.Minute);
                                    strSms.Append("分的空载里程");
                                    strSms.Append(nowdistance);
                                    strSms.Append("公里已使用完！");                                    
                                }
                                else //今天白天
                                {
                                    startTime = stime2;
                                    endTime = etime2;
                                    nowdistance = distance2;

                                    strSms.Append("今天");
                                    strSms.Append(stime2.Hour);
                                    strSms.Append("点");
                                    strSms.Append(stime2.Minute);
                                    strSms.Append("分到");
                                    strSms.Append(etime2.Hour);
                                    strSms.Append("点");
                                    strSms.Append(etime2.Minute);
                                    strSms.Append("分的空载里程");
                                    strSms.Append(nowdistance);
                                    strSms.Append("公里已使用完！");   
                                }
                            }
                            else//开始时间小于结束时间
                            {
                                if (DateTime.Now.TimeOfDay > stime1.TimeOfDay
                                && DateTime.Now.TimeOfDay <= etime1.TimeOfDay)//今天白天
                                {
                                    startTime = stime1;
                                    endTime = etime1;
                                    nowdistance = distance1;

                                 

                                    strSms.Append("今天");
                                    strSms.Append(stime1.Hour);
                                    strSms.Append("点");
                                    strSms.Append(stime1.Minute);
                                    strSms.Append("分到");
                                    strSms.Append(etime1.Hour);
                                    strSms.Append("点");
                                    strSms.Append(etime1.Minute);
                                    strSms.Append("分的空载里程");
                                    strSms.Append(nowdistance);
                                    strSms.Append("公里已使用完！");     
                                }
                                else//今天晚上到明天上午
                                {
                                    startTime = stime2;
                                    endTime = etime2;
                                    nowdistance = distance2;

                                    if(stime2>etime2)
                                    {
                                     
                                    
                                        strSms.Append("今天");
                                        strSms.Append(stime2.Hour);
                                        strSms.Append("点");
                                        strSms.Append(stime2.Minute);
                                        strSms.Append("分到明天");
                                        strSms.Append(etime2.Hour);
                                        strSms.Append("点");
                                        strSms.Append(etime2.Minute);
                                        strSms.Append("分的空载里程");
                                        strSms.Append(nowdistance);
                                        strSms.Append("公里已使用完！");    
                                    }
                                    else
                                    {
                                        strSms.Append("今天");
                                        strSms.Append(stime2.Hour);
                                        strSms.Append("点");
                                        strSms.Append(stime2.Minute);
                                        strSms.Append("分到");
                                        strSms.Append(etime2.Hour);
                                        strSms.Append("点");
                                        strSms.Append(etime2.Minute);
                                        strSms.Append("分的空载里程");
                                        strSms.Append(nowdistance);
                                        strSms.Append("公里已使用完！");    
                                    }

                                    
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.ToString());
                        strSms = new StringBuilder();
                    }
                }


 

                //string content = "请注意，您今天"+startTime.TimeOfDay.Hours+"点"
                //    +startTime.TimeOfDay.Minutes+"到"+endTime.TimeOfDay.Hours
                //    +"点"+endTime.TimeOfDay.Minutes+"的空载里程为"+nowdistance;
                string content = "请注意您的空载里程已用完！";
                if (strSms.ToString() != "")
                    content = strSms.ToString();
                string result = UnionSMS.SendSMS(phoneNumber, content);
                if (result.Contains("发送短信成功"))
                {
                    Response.Write("1");
                }
                else 
                {
                    Response.Write("0");
                }
            }
            else
            {
                Response.Write("-1");
            }
        }
    }
}